package org.game.module.event.listener;

import org.cfg.EquipmentCfg;
import org.cfg.EquipmentSuitCfg;
import org.cfg.cache.EquipmentSuitCfgCache;
import org.constant.EventConditionType;

import java.util.List;

import org.base.BaseGoods;
import org.cache.GoodsCfgCache;
import org.cfg.ItemTimeCfg;
import org.constant.EventConditionKey;
import org.constant.EventType;
import org.game.db.bean.Item;
import org.game.db.bean.Player;
import org.game.manager.EquipmentManager;
import org.game.plugin.event.GameEvent;
import org.game.plugin.event.GameEventListener;
import org.game.plugin.event.GameEventStack;
import org.game.plugin.event.GameEventType;

import com.sojoys.artifact.tools.ToolMap;
@GameEventType(EventType.ITEM_EVENT)
public class ItemListener implements GameEventListener {

	@Override
	public void onEvent(GameEvent event) {
		GameEventStack eventStack = event.peek();
		switch (eventStack.getConditionType()) {
			case EventConditionType.HOLD_HERO:{
				if (checkTriggerType(event.getIn(), EventConditionType.HOLD_HERO)){
					boolean bool = true;
					bool &= verify_eq(EventConditionKey.HERO_CID, event.getIn(), eventStack.getParams());
					bool &= verify_ge(EventConditionKey.QUALITY, event.getIn(), eventStack.getParams());
					if (bool) {
						eventStack.changeProgress(1);
					}
				}
				break;
			}
			case EventConditionType.HOLD_ITEM:
			case EventConditionType.ADD_ITEM:{
				if (checkTriggerType(event.getIn(), EventConditionType.ADD_ITEM)){
					boolean bool = true;
					bool &= verify_eq(EventConditionKey.ITEM_CID, event.getIn(), eventStack.getParams());
					bool &= verify_eq(EventConditionKey.SUPER_TYPE, event.getIn(), eventStack.getParams());
					bool &= verify_eq(EventConditionKey.SUB_TYPE, event.getIn(), eventStack.getParams());
					bool &= verify_eq(EventConditionKey.SMALL_TYPE, event.getIn(), eventStack.getParams());
					bool &= verify_eq(EventConditionKey.QUALITY, event.getIn(), eventStack.getParams());
					bool &= verify_not_eq(EventConditionKey.EXCLUDE, event.getIn(), eventStack.getParams());
					int count = ToolMap.getInt(EventConditionKey.COUNT, event.getIn());
					if (bool) {
						eventStack.changeProgress(count);
					}
				}
				break;
			}
			case EventConditionType.DEL_ITEM:{
				if (checkTriggerType(event.getIn(), eventStack.getConditionType())){
					boolean bool = true;
					bool &= verify_eq(EventConditionKey.ITEM_CID, event.getIn(), eventStack.getParams());
					bool &= verify_not_eq(EventConditionKey.EXCLUDE, event.getIn(), eventStack.getParams());
					int count = ToolMap.getInt(EventConditionKey.COUNT, event.getIn());
					if (bool) {
						eventStack.changeProgress(count);
					}
				}
				break;
			}
			case EventConditionType.EQUIP_COVER_COUNT:{
				if (checkTriggerType(event.getIn(), EventConditionType.ADD_ITEM)){
					Player player = event.getPlayer();
					boolean bool = true;
					int goodsId = ToolMap.getInt(EventConditionKey.ITEM_CID, event.getIn());
					int coverId = ToolMap.getInt(EventConditionKey.COVER_ID, eventStack.getParams());
					BaseGoods goods = GoodsCfgCache.me().get(goodsId);
					if (goods instanceof EquipmentCfg) {
						EquipmentCfg equip = (EquipmentCfg) goods;
						if (equip.getSuit() == coverId) {
							bool &= EquipmentManager.me().checkHaveCover(player, coverId);
						}else if (coverId == 0){
							int suitCount = checkEquipmentSuitCount(player, eventStack.getMaxProgress());
							bool &= suitCount > eventStack.getProgress();
						}else{
							bool &= false;
						}
					}else {
						bool&=false;
					}

					if (bool) {
						eventStack.changeProgress(1);
					}
				}
				break;
			}
		}
	}

	@Override
	public void initEventCondition(Player player, GameEventStack eventStack) {
		switch (eventStack.getConditionType()) {
		case EventConditionType.HOLD_HERO:{
			int heroId = ToolMap.getInt(EventConditionKey.HERO_CID, eventStack.getParams());
			int quality = ToolMap.getInt(EventConditionKey.QUALITY, eventStack.getParams());
			int count = player.getPlayerProxy().getHeroCountByHeroIdAndQuality(heroId, quality);
			eventStack.setProgress(count);
		}
			break;
		case EventConditionType.HOLD_ITEM:{
			List<Item> items = player.getItemsFkPlayerId();
			BaseGoods goods = null;
			int count = 0;
			for (Item item : items) {
				goods = GoodsCfgCache.me().get(item.getCid());
				if (goods != null && goods instanceof ItemTimeCfg) {
					ItemTimeCfg cfg = (ItemTimeCfg)goods;
					goods = GoodsCfgCache.me().get(cfg.getItemId());
				}
				if (goods == null) {
					continue;
				}
				int itemId = ToolMap.getInt(EventConditionKey.ITEM_CID, eventStack.getParams(),0);
				int superType = ToolMap.getInt(EventConditionKey.SUPER_TYPE, eventStack.getParams(),0);
				int subType = ToolMap.getInt(EventConditionKey.SUB_TYPE, eventStack.getParams(),0);
				int smallType = ToolMap.getInt(EventConditionKey.SMALL_TYPE, eventStack.getParams(),0);
				int quality = ToolMap.getInt(EventConditionKey.QUALITY, eventStack.getParams(),0);
				if (itemId !=0 && goods.getId() != itemId) {
					continue;
				}
				if (superType !=0 && goods.getSuperType() != superType) {
					continue;
				}
				if (subType !=0 && goods.getSubType() != subType) {
					continue;
				}
				if (smallType !=0 && goods.getSmallType() != smallType) {
					continue;
				}
				if (quality !=0 && goods.getQuality() != quality) {
					continue;
				}
				count++;
			}
			eventStack.setProgress(count);
		}
			break;
		}
	}

	/**
	 * 检查玩家拥有套装数量是否达到指定数量
	 * @param player
	 * @param count 指定数量
	 * @return
	 */
	private int checkEquipmentSuitCount(Player player,int count){
		List<EquipmentSuitCfg> suitCfgList = EquipmentSuitCfgCache.me().getAll();
		int actualCount = 0;
		for (EquipmentSuitCfg equipmentSuitCfg : suitCfgList) {
			if (EquipmentManager.me().checkHaveCover(player,equipmentSuitCfg.getId())){
				actualCount++;
				if (actualCount >= count)
					return actualCount;
			}
		}
		return actualCount;
	}

}
